package com.cskaoyan.market.aspect;

import com.cskaoyan.market.util.ValidationUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;

@Component
@Aspect
public class ValidAspect {
    @Pointcut("@annotation(com.cskaoyan.market.annotation.ValidParam)")
    public void pt(){}

    @Before("pt()")
    public void beforeAdvice(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            if(arg instanceof BindingResult){
                BindingResult bindingResult = (BindingResult) arg;
                //参数校验的逻辑和业务逻辑进行解耦
                ValidationUtils.validate2(bindingResult);
            }
        }
    }
}
